﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm_Mes.CheckManagement
{
    public partial class AddCheckItemForm : DevExpress.XtraEditors.XtraForm
    {
        public AddCheckItemForm()
        {
            InitializeComponent();
            this.Load += AddCheckItemForm_Load;
            btnSubmit.Click += btnSubmit_Click;
            btnCancel.Click += btnCancel_Click;
        }

        private async void AddCheckItemForm_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterParent; // 窗体居中

            // 1. 自动获取并设置项目编号
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    string url = "http://localhost:5295/api/CheckItem/GetCheckItemCode";
                    var code = await client.GetStringAsync(url);
                    txtCheckItemCode.Text = code.Trim('"');
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"获取项目编号失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }

            // 2. 填充分类下拉框
            cmbClassification.Properties.Items.Clear();
            cmbClassification.Properties.Items.Add("一级分类");
            cmbClassification.Properties.Items.Add("二级分类");
            cmbClassification.SelectedIndex = 0; // 默认选中第一项
        }

        // 提交按钮的点击事件
        private async void btnSubmit_Click(object sender, EventArgs e)
        {
            // 1. 输入校验
            if (string.IsNullOrWhiteSpace(txtCheckItemName.Text))
            {
                XtraMessageBox.Show("项目名称不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCheckItemName.Focus();
                return;
            }
            if (cmbClassification.SelectedItem == null)
            {
                XtraMessageBox.Show("请选择一个分类！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 2. 构造请求参数
            var requestData = new
            {
                checkItenCode = txtCheckItemCode.Text,
                checkItemName = txtCheckItemName.Text.Trim(),
                classificationName = cmbClassification.SelectedItem.ToString()
            };

            // 3. 调用后端API
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    string url = "http://localhost:5295/api/CheckItem/AddCheckItem"; // 确认你的新增API地址
                    HttpResponseMessage response = await client.PostAsJsonAsync(url, requestData);

                    if (response.IsSuccessStatusCode)
                    {
                        XtraMessageBox.Show("新增成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        // 关键步骤：设置成功结果，然后关闭窗体
                        this.DialogResult = DialogResult.OK;
                        this.Close(); // 必须调用Close()
                    }
                    else
                    {
                        string errorMsg = await response.Content.ReadAsStringAsync();
                        XtraMessageBox.Show($"新增失败: {response.ReasonPhrase}\n{errorMsg}", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"请求异常: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 取消按钮的点击事件
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}